<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="登录账号" prop="loginName">
      <el-input v-model="dataForm.loginName" placeholder="登录账号"></el-input>
    </el-form-item>
    <el-form-item label="登录IP地址" prop="ipaddr">
      <el-input v-model="dataForm.ipaddr" placeholder="登录IP地址"></el-input>
    </el-form-item>
    <el-form-item label="登录地点" prop="loginLocation">
      <el-input v-model="dataForm.loginLocation" placeholder="登录地点"></el-input>
    </el-form-item>
    <el-form-item label="浏览器类型" prop="browser">
      <el-input v-model="dataForm.browser" placeholder="浏览器类型"></el-input>
    </el-form-item>
    <el-form-item label="操作系统" prop="os">
      <el-input v-model="dataForm.os" placeholder="操作系统"></el-input>
    </el-form-item>
    <el-form-item label="在线状态on_line在线off_line离线" prop="status">
      <el-input v-model="dataForm.status" placeholder="在线状态on_line在线off_line离线"></el-input>
    </el-form-item>
    <el-form-item label="session创建时间" prop="startTimestamp">
      <el-input v-model="dataForm.startTimestamp" placeholder="session创建时间"></el-input>
    </el-form-item>
    <el-form-item label="session最后访问时间" prop="lastAccessTime">
      <el-input v-model="dataForm.lastAccessTime" placeholder="session最后访问时间"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="expireTime">
      <el-input v-model="dataForm.expireTime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="token" prop="token">
      <el-input v-model="dataForm.token" placeholder="token"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          loginName: '',
          ipaddr: '',
          loginLocation: '',
          browser: '',
          os: '',
          status: '',
          startTimestamp: '',
          lastAccessTime: '',
          expireTime: '',
          updateTime: '',
          token: '',
          userId: ''
        },
        dataRule: {
          loginName: [
            { required: true, message: '登录账号不能为空', trigger: 'blur' }
          ],
          ipaddr: [
            { required: true, message: '登录IP地址不能为空', trigger: 'blur' }
          ],
          loginLocation: [
            { required: true, message: '登录地点不能为空', trigger: 'blur' }
          ],
          browser: [
            { required: true, message: '浏览器类型不能为空', trigger: 'blur' }
          ],
          os: [
            { required: true, message: '操作系统不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '在线状态on_line在线off_line离线不能为空', trigger: 'blur' }
          ],
          startTimestamp: [
            { required: true, message: 'session创建时间不能为空', trigger: 'blur' }
          ],
          lastAccessTime: [
            { required: true, message: 'session最后访问时间不能为空', trigger: 'blur' }
          ],
          expireTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          token: [
            { required: true, message: 'token不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/sysuseronline/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.loginName = data.sysUserOnline.loginName
                this.dataForm.ipaddr = data.sysUserOnline.ipaddr
                this.dataForm.loginLocation = data.sysUserOnline.loginLocation
                this.dataForm.browser = data.sysUserOnline.browser
                this.dataForm.os = data.sysUserOnline.os
                this.dataForm.status = data.sysUserOnline.status
                this.dataForm.startTimestamp = data.sysUserOnline.startTimestamp
                this.dataForm.lastAccessTime = data.sysUserOnline.lastAccessTime
                this.dataForm.expireTime = data.sysUserOnline.expireTime
                this.dataForm.updateTime = data.sysUserOnline.updateTime
                this.dataForm.token = data.sysUserOnline.token
                this.dataForm.userId = data.sysUserOnline.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/sysuseronline/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'loginName': this.dataForm.loginName,
                'ipaddr': this.dataForm.ipaddr,
                'loginLocation': this.dataForm.loginLocation,
                'browser': this.dataForm.browser,
                'os': this.dataForm.os,
                'status': this.dataForm.status,
                'startTimestamp': this.dataForm.startTimestamp,
                'lastAccessTime': this.dataForm.lastAccessTime,
                'expireTime': this.dataForm.expireTime,
                'updateTime': this.dataForm.updateTime,
                'token': this.dataForm.token,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
